
var totalFruit = function(fruits) {
   const n = fruits.length
   let l=0,
       arr=[fruits[0]],
       subLength=0,
       m=0;//前一类水果结束的位置
    for(let r=1;r<n;r++){
        if(!arr.includes(fruits[r])){
            if(arr.length<2){
                arr[1]=fruits[r]
            }else{
                l=m
                arr[0]=arr[r-1]
                arr[1]=fruits[r]
            }
        }
        if(fruits[m]!==fruits[r]){
            m=r
        }
        subLength=Math.max(subLength,r-l+1)
    }
    return subLength
};
